package com.ruoyi.douyin.service.open.imp;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.ruoyi.common.constant.ConstantDouYinBase;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.vo.AuthVo;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.douyin.service.open.VideoOpenDataService;
import com.ruoyi.douyin.utils.HttpUtils;
import com.ruoyi.douyin.vo.VideoOpenDataVideoDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.URLEncoder;
import java.util.HashMap;

@Service
public class VideoOpenDataServiceImp implements VideoOpenDataService {
    @Autowired
    private RedisCache redisCache;

    /**
     * 视频30天的基础数据
     */
    @Override
    public AjaxResult baseData(String openId, String itemId) {
        AuthVo authVo = redisCache.getCacheObject(openId);
        if (authVo == null) return AjaxResult.error("抖音授权过期");
        try {
            String url = ConstantDouYinBase.Url.BASE_URL.getUrl() + ConstantDouYinBase.Url.DATA_OPEN_VIDEO_BASE_DATA.getUrl();
            HashMap<String, Object> param = new HashMap<>();
            param.put("open_id", openId);
            param.put("access_token", authVo.getAccessToken());
            param.put("item_id", URLEncoder.encode(itemId, "UTF-8"));
            String paramUrl = HttpUtils.paramAdd(url, param);
            String result = HttpUtil.get(paramUrl);
            return AjaxResult.success("success", JSON.parse(result));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }

    /**
     * 视频的其他数据
     */
    @Override
    public AjaxResult otherData(VideoOpenDataVideoDataVo videoOpenDataVideoDataVo, String type) {
        AuthVo authVo = redisCache.getCacheObject(videoOpenDataVideoDataVo.getOpenId());
        if (authVo == null) return AjaxResult.error("抖音授权过期");
        String url = null;
        switch (type) {
            case "like":
                url = ConstantDouYinBase.Url.BASE_URL.getUrl() + ConstantDouYinBase.Url.DATA_OPEN_VIDEO_LIKE_DATA.getUrl();
                break;
            case "comment":
                url = ConstantDouYinBase.Url.BASE_URL.getUrl() + ConstantDouYinBase.Url.DATA_OPEN_VIDEO_COMMENT_DATA.getUrl();
                break;
            case "play":
                url = ConstantDouYinBase.Url.BASE_URL.getUrl() + ConstantDouYinBase.Url.DATA_OPEN_VIDEO_PLAYER_DATA.getUrl();
                break;
            case "share":
                url = ConstantDouYinBase.Url.BASE_URL.getUrl() + ConstantDouYinBase.Url.DATA_OPEN_VIDEO_SHARE_DATA.getUrl();
                break;
        }
        try {
            HashMap<String, Object> param = new HashMap<>();
            param.put("open_id", videoOpenDataVideoDataVo.getOpenId());
            param.put("date_type", videoOpenDataVideoDataVo.getDataType());
            param.put("access_token", authVo.getAccessToken());
            param.put("item_id", URLEncoder.encode(videoOpenDataVideoDataVo.getItemId(), "UTF-8"));
            String paramUrl = HttpUtils.paramAdd(url, param);
            String result = HttpUtil.get(paramUrl);
            return AjaxResult.success("success", JSON.parse(result));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }
}
